package com.yuanhua.xcxmng.wechat.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.yuanhua.xcxmng.wechat.entity.User;
import com.yuanhua.xcxmng.wechat.entity.UserData;
import com.yuanhua.xcxmng.wechat.entity.UserDataQuery;
import com.yuanhua.xcxmng.wechat.mapper.UserDataMapper;
import com.yuanhua.xcxmng.wechat.service.BaseService;
import com.yuanhua.xcxmng.wechat.service.UserDataService;
import com.yuanhua.xcxmng.wechat.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDataServiceImpl extends BaseService implements UserDataService {

    @Autowired
    private UserDataMapper userDataMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        try {
            return userDataMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public int insert(UserData record) {
        try {
            return userDataMapper.insert(record);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public int insertSelective(UserData record) {
        try {
            return userDataMapper.insertSelective(record);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public UserData selectByPrimaryKey(Long id) {
        try {
            return userDataMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public List<UserData> selectLastThree(UserDataQuery userDataQuery) {
        try {
            return userDataMapper.selectLastThree(userDataQuery);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public List<UserData> queryPage(UserDataQuery userDataQuery, int next) {
        try {
            Page<User> pageUser = PageHelper.offsetPage(next, 10, true);
            List<UserData> result = userDataMapper.queryPage(userDataQuery);
            if (next >= pageUser.getTotal()) {
                return Lists.newArrayList();
            }
            return result;
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            return Lists.newArrayList();
        } finally {
            PageHelper.clearPage();
        }
    }

    @Override
    public List<UserData> queryReport(UserDataQuery userDataQuery) {
        try {
            return userDataMapper.queryPage(userDataQuery);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public int updateByPrimaryKeySelective(UserData record) {
        try {
            return userDataMapper.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public int updateByPrimaryKey(UserData record) {
        try {
            return userDataMapper.updateByPrimaryKey(record);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }
}
